
layui.use(['form', 'layer', 'laydate', 'table', 'upload'], function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        upload = layui.upload,
        table = layui.table;
    var datalist_url = $('.datalist_url').val();
    var detail_url = $('.detail_url').val();
    var delete_url = $('.delete_url').val();
    var save_url = $('.save_url').val();

    laydate.render({
        elem: '#test16',
        type: 'datetime',
        range: '到',
        format: 'yyyy年M月d日H时m分s秒'
    });

    var tableIns = table.render({
        elem: '#dataTable',
        url : datalist_url,
        page : true,
        cellMinWidth : 95,
        //height : "full-100",
        limit : 10,
        limits : [10, 20, 50, 100],
        id : "tableList",
        initSort: {
            field: 'create_date' // 排序字段，对应 cols 设定的各字段名
            ,type: 'desc' // 排序方式  asc: 升序、desc: 降序、null: 默认排序
        },
        cols : [[
            /* {type: "checkbox", fixed:"left", width:50},*/
            {field: 'order_id', title: '订单ID'},
            {field: 'create_date', title: '创建日期', align:'center',sort: true},
            {field: 'product_name', title: '售后商品', align:'center'},
            {field: 'old_total', title: '订单金额',align:"center",templet:function(data){
                return formatMoney(data.old_total/100,1)
            }},
            {field: 'total', title: '付款方式', align:'center'},
            {field: 'refund_fee', title: '退款金额', align:'center',templet:function(data){
                return formatMoney(data.refund_fee/100,1)
            }},
            {field: 'account_number', title: '售后人', align:'center'},
            {field: 'damage_type', title: '问题类型', align:'center',templet:function(data){
                if(data.damage_type == 1){
                    return '商品问题'
                }else if(data.damage_type == 2){
                    return '物流问题'
                }
            }},
            {field: 'status', title: '状态', align:'center',templet:function(data){
                if(data.status == 1){
                    return '用户申请中'
                }else if(data.status == 2){
                    return '待退款'
                }else if(data.status == 3){
                    return '后台拒绝售后'
                }else if(data.status == 4){
                    return '同意售后待用户退货'
                }else if(data.status == 5){
                    return '用户退货在途'
                }else if(data.status == 6){
                    return '用户退货后台已收货'
                }else if(data.status == 7){
                    return '退款成功'
                }else {
                    return '用户取消售后'
                }
            }},
            {title: '操作', fixed:"right", align:"center",templet:function(data){
                var html = '';
                html += '<a type="button" class="layui-btn layui-btn-xs layui-btn-normal" href="'+detail_url+'/id/'+data.id+'"><i class="layui-icon">&#xe60a;</i>详情</a>';
                return html;
            }}
        ]]
    });

    // Table 列表
    var tableDetail = table.render({
        elem: '#dataTable01',
        url : detail_url,
        page : false,
        cellMinWidth : 95,
        //height : "full-100",
        id : "tableList",
        initSort: {
            field: 'create_date' // 排序字段，对应 cols 设定的各字段名
            ,type: 'desc' // 排序方式  asc: 升序、desc: 降序、null: 默认排序
        },
        cols : [[
            /* {type: "checkbox", fixed:"left", width:50},*/
            {field: 'name', title: '商品名称'},
            {field: 'damage_type', title: '问题类型', align:'center',templet:function(data){
                if(data.damage_type == 1){
                    return '商品问题'
                }else {
                    return '物流问题'
                }
            }},
            {field: 'reason', title: '问题原因', align:'center',templet:function(data){
                if(data.reason == 1){
                    return '过期'
                }else if(data.reason == 2) {
                    return '质量'
                }else if(data.reason == 3) {
                    return '损耗'
                }else if(data.reason == 4) {
                    return '其它'
                }else if(data.reason == 5) {
                    return '拼团失败，自动退款'
                }
            }},
            {field: 'count', title: '问题商品数量',align:"center"},
            {field: 'reason_des', title: '问题描述', align:'center'},
            {field: 'imgs', title: '问题图片', align:'center',templet:function(data){
                var html = '';
                if(data.imgs != null){
                    var imgsarr = data.imgs.split(',');

                    for(var i=0;i<imgsarr.length;i++){
                        html += '<a href="'+imgsarr[i]+'" target="_blank"><img src="'+imgsarr[i]+'" style="width: 80px;" /></a>'
                    }
                }

                return html
            }},
        ]]
    });

    // 搜索
    $(".search_btn").on("click", function(){
        if ($('.order_no').val() != ''){
            var order_no = $('.order_no').val();
        }
        if ($('.member_id').val() != '') {
            var member_id = $('.member_id').val();
        }
        if ($('.deliver_method').val() != '') {
            var deliver_method = $('.deliver_method').val();
        }
        if ($('.status').val() != '') {
            var status = $('.status').val();
        }
        if ($('.time').val() != '') {
            var time = $('.time').val();
        }
        table.reload("tableList", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                order_no: order_no,
                member_id: member_id,
                deliver_method: deliver_method,
                status: status,
                time: time,
            }
        });
    });

    $('.refuse_aftersale').on('click', function(){

        layer.open({
            type: 1
            , anim: 1
            , title: '拒绝售后'
            ,btn: ['确定']
            , closeBtn: 1
            , shade: 0.2
            , shadeClose: true // 是否点击遮罩关闭
            , skin: 'layui-anim layui-anim-rl layui-layer-Elasticframe'
            , area: ['600px','400px']
            , content: $('#refuse')
            ,yes: function(index, layero){
                var content = $('#content').val();
                var aftersale_id = $('#aftersale_id').val();
                if(aftersale_id.trim() == ''){
                    layer.msg('缺少售后id参数');
                }else if(content.trim() == ''){
                    layer.msg('请填写拒绝原因');
                }else if(content.trim().length > 50){
                    layer.msg('最大输入50个字');
                }else {
                    $.ajax({
                        url: '/admin/Aftersale/refuse_aftersale',
                        data: {'id':aftersale_id,'content':content},
                        type: "POST",
                        dataType: "json",
                        success: function (res) {
                            if (res.code == 1) {
                                layer.msg(res.msg);
                                layer.close(index);
                                setTimeout(function () {
                                    window.location.href = res.url
                                }, 2000)
                                //
                            } else {
                                layer.msg(res.msg);
                            }
                        },
                        error: function (data) {
                            layer.msg("服务器无响应");
                        }
                    });

                }
                //按钮【按钮一】的回调
            }
        });
        $(".layui-layer-shade").css("display","none");
        //其中：layero是弹层的DOM对象
        // $('#view-modal').modal('toggle');

    });


    $("#help_refuse").on("click",function () {

        var content = $('#help_refuse_input').val();

        layer.tips('拒绝原因：'+content, '#help_refuse', {
            tips: [1, '#23262E'],
            time: 4000
        });

    })

    $(".agree_aftersale").on("click",function () {
        var id = $('#aftersale_id').val()
        layer.alert('请仔细检查订单！', {
            skin: 'layui-layer-molv' //样式类名  自定义样式
            ,closeBtn: 1    // 是否显示关闭按钮
            ,anim: 1 //动画类型
            ,title:'确定同意售后吗？'
            ,area: ['420px', '240px'] //宽高
            ,btn: ['确定'] //按钮
            ,icon: 7    // icon
            ,yes:function(){
                $.ajax({
                    url: '/admin/Aftersale/agree_aftersale',
                    data: {'id':id},
                    type: "POST",
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 1) {
                            layer.msg(res.msg);
                            window.location.href = res.url
                        } else {
                            layer.msg(res.msg);
                        }
                    },
                    error: function (data) {
                        layer.msg("服务器无响应");
                    }
                });
            }
        });
    })

    $(".confirm_refund").on("click",function () {
        var id = $('#aftersale_id').val()
        layer.alert('请仔细核对退款单！', {
            skin: 'layui-layer-molv' //样式类名  自定义样式
            ,closeBtn: 1    // 是否显示关闭按钮
            ,anim: 1 //动画类型
            ,title:'确定退款吗？'
            ,area: ['420px', '240px'] //宽高
            ,btn: ['确定'] //按钮
            ,icon: 7    // icon
            ,yes:function(){
                $.ajax({
                    url: '/admin/Aftersale/confirm_refund',
                    data: {'id':id},
                    type: "POST",
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 1) {
                            layer.msg(res.msg);
                            window.location.href = res.url
                        } else {
                            layer.msg(res.msg);
                        }
                    },
                    error: function (data) {
                        layer.msg("服务器无响应");
                    }
                });
            }
        });
    })

    $(".confirm_receipt").on("click",function () {
        var id = $('#aftersale_id').val()
        layer.alert('请您确定已经收到了用户寄回来的货品！', {
            skin: 'layui-layer-molv' //样式类名  自定义样式
            ,closeBtn: 1    // 是否显示关闭按钮
            ,anim: 1 //动画类型
            ,title:'确定收货吗？'
            ,area: ['420px', '240px'] //宽高
            ,btn: ['确定'] //按钮
            ,icon: 7    // icon
            ,yes:function(){
                $.ajax({
                    url: '/admin/Aftersale/confirm_receipt',
                    data: {'id':id},
                    type: "POST",
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 1) {
                            layer.msg(res.msg);
                            window.location.href = res.url
                        } else {
                            layer.msg(res.msg);
                        }
                    },
                    error: function (data) {
                        layer.msg("服务器无响应");
                    }
                });
            }
        });
    })

    $('.view_logistics').on('click', function(){
        var aftersale_id = $('#aftersale_id').val();
        var content = '/admin/Aftersale/show_wuliu/aftersale_id/' + aftersale_id
        var index = layer.open({
            type: 2
            , anim: 1
            , title: '退货物流'
            , shade: 0.2
            //, shadeClose: true // 是否点击遮罩关闭
            , skin: 'layui-anim layui-anim-rl layui-layer-adminRight'
            , area: ['900px','700px']
            , top: 0
            , content: content
        });

    });

    // 批量删除
    $(".delAll_btn").on("click", function(){
        var checkStatus = table.checkStatus('tableList'),
            data = checkStatus.data,
            ids = [];
        if(data.length > 0) {
            for (var i in data) {
                ids.push(data[i].id);
            }
            layer.confirm('确定删除选中的数据？', {icon: 3, title: '提示信息'}, function (index) {
                $.ajax({
                    url: delete_url,
                    data: {"ids": ids},
                    type: "POST",
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 1) {
                            layer.msg('删除成功', {icon: 1});
                            tableIns.reload();
                            layer.close(index);
                        } else {
                            layer.msg(res.msg);
                        }
                    },
                    error: function (data) {
                        layer.msg("服务器无响应");
                    }
                });
            })
        }else{
            layer.msg("请选择需要删除的数据");
        }
    });

    $('.add_btn').on('click', function(){
        formHtml();
    });

    form.on('select(type)', function(data){
        var type = data.value;
        if (type == 1 || type == 2 || type == 3) {
            $('.amount').removeClass("layui-hide");
            $('.strength').addClass("layui-hide");
        }else{
            $('.strength').removeClass("layui-hide");
            $('.amount').addClass("layui-hide");
        }
    });


    // 列表操作
    table.on('tool(dataTable)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        // 删除
        if (layEvent === 'del') {
            layer.confirm('确定删除此数据？',{icon:3, title:'提示信息'},function(index){
                $.ajax({
                    url: delete_url,
                    data: {"id": data.id},
                    type: "POST",
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 1) {
                            layer.msg('已删除', {icon: 1, time: 1000});
                            tableIns.reload();
                            layer.close(index);
                        } else {
                            layer.msg(res.msg);
                        }
                    },
                    error: function (data) {
                        layer.msg("服务器无响应");
                    }
                });
            });
        } else if(layEvent === 'detail'){
            /*layer.open({ type: 1,
                //此处以iframe举例
                area: ['690px', '350px'],
                area: ['750px', '75%'],
                shade: 0.5,
                maxmin: true,
                //url: detail_url + '?id=' + data.id
                content: ['detail.html','yes'],
            });*/

            /*location.href = detail_url + '?id=' + data.id*/
        }
    });

    // Form 表单
    function formHtml(data){
        var content = detail_url;

        if(data){
            content = detail_url + '?id=' + data.id;
        }
        // 右侧面板
        var index = layer.open({
            type: 2,
            anim: -1,
            title: false,
            closeBtn: false,
            offset: 'r',
            shade: 0.2,
            shadeClose: true, // 是否点击遮罩关闭
            skin: 'layui-anim layui-anim-rl layui-layer-adminRight',
            area: '1200px',
            top: 0,
            content: content
        });
    }

    // 文件上传
    upload.render({
        elem: '.linkLogo',
        url: '../../json/linkLogo.json',
        method : "post",
        done: function(res, index, upload){
            var num = parseInt(4*Math.random());  //生成0-4的随机数，随机显示一个头像信息
            $('.linkLogoImg').attr('src',res.data[num].src);
            $('.linkLogo').css("background","#fff");
        }
    });

    // 表单提交
    form.on("submit(laySave)", function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});

        $.ajax({
            url: save_url,
            data: data.field,
            type: "post",
            dataType: "json",
            success: function(res){
                top.layer.close(index);
                top.layer.msg(res.msg);
                if(res.code == 1){
                    // var index = parent.layer.getFrameIndex(window.name);
                    // // 关闭当前frame
                    // parent.layer.close(index);

                    layer.closeAll("iframe");
                    //parent.window.tableIns.reload();
                    parent.layui.table.reload('tableList', {});
                    // 刷新父页面
                    //parent.location.reload();
                }
            },
            error:function(data){
                layer.msg("服务器无响应");
            }
        });

    })
});